<template>
    <div>
        <el-card class="box-card" style="width: 400px; position: absolute; top:50%;left: 50%;transform: translate(-50%,-50%)">
            <div slot="header" class="clearfix">
                <span><i class="el-icon-key" style="color: chocolate"></i>用户登录</span>
            </div>

            <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" suffix-icon="el-icon-user"></el-input>
                </el-form-item>
                <el-form-item label="用户密码" prop="password">
                    <el-input v-model="form.password" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="listZm('form')">登录</el-button>
                    <el-button @click="restForm('form')">重置</el-button>
                </el-form-item>
            </el-form>

        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
          return{
              form:{
                  username:"root",
                  password:"654321"
              },
              rules: {
                  username: [
                      { required: true, message: '请输入活动名称', trigger: 'blur' },
                      { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                  ],
                  password: [
                      { required: true, message: '请输入密码', trigger: 'blur' },
                      { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                  ]
              }
          }
        },
        methods:{
            listZm(){
                this.$refs["form"].validate((valid) => {
                    if (valid) {
                        this.$http.post("listByZm",this.form).then(res=>{
                            if (res.data.success){
                                let json = JSON.stringify(res.data.target);
                                sessionStorage.setItem("sessionStorage_user",json)
                                this.$router.push("/index")
                            }else {
                                this.$message.error(res.data.error)
                            }
                        })
                    } else {
                        return false;
                    }
                })

            },
            restForm(){
                this.$refs["form"].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>